package flex.cc.store.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;

import flex.cc.PublicUtil;
import flex.cc.basicinformation.entity.ValueAddedServiceProviderEntity;
import flex.cc.basicinformation.service.StoreActivityConfigService;
import flex.cc.basicinformation.service.ValueAddedServiceProviderService;
import flex.cc.core.support.BaseController;
import flex.cc.store.dto.entity.StoreActivityConfigEntity;
import flex.cc.store.dto.vo.ReservationOrderVo;
import flex.cc.store.service.ReservationOrderService;
import flex.cc.system.service.DictCodeService;
import flex.cc.system.vo.DictOptionVo;
import flex.cc.util.FileDownloadUtil;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 * @author WANGCONG
 * 2022年2月25日下午16:51:36
 * @desc 预约服务单管理;t_drive_order
 */
@RestController
@RequestMapping(value = "/reservation/order")
public class ReservationOrderController extends BaseController{

	@Resource
	private ReservationOrderService reservationOrderService;
	@Resource
	private StoreActivityConfigService storeActivityConfigService;
	@Resource
	private DictCodeService dictCodeService;
	@Resource
	private ValueAddedServiceProviderService valueAddedServiceProviderService;
	
	/**
	 * 
	 * @author WANGCONG
	 * 2022年3月1日上午09:26:36
	 * @desc 分页查询
	 */
	@PostMapping(value = "/queryListWithPage")
    @ApiOperation(httpMethod = "POST", value = "查询预约服务订单列表")
    public Wrapper<IPage<ReservationOrderVo>> queryOrderListWithPage(@ApiParam(name = "order", value = "预约服务订单信息") @RequestBody Map<String, Object> reqVo) {
        logger.info("查询预约服务订单列表={}", reqVo);
        ReservationOrderVo vo = initVo(reqVo);
        IPage<ReservationOrderVo> page = reservationOrderService.queryListWithPage(vo);
        return WrapMapper.ok(page);
    }
	
	/**
	 * 
	 * @author WANGCONG
	 * 2022年3月1日上午10:00:49
	 * @desc 导出
	 */
	@PostMapping(value = "/exportAll")
    @ApiOperation(httpMethod = "POST", value = "预约服务订单导出")
    public void exportAll(HttpServletRequest request, HttpServletResponse response,@ApiParam(name = "reqVo", value = "请求信息") @RequestBody Map<String, Object> reqVo){
        logger.info("预约服务订单导出requestVo={}", reqVo);
        ReservationOrderVo vo = initVo(reqVo);
        boolean hasPerms = (boolean) reqVo.get("hasPerms");
        String fileParentPath = System.getProperty("user.dir") + File.separator + "order" + File.separator + System.currentTimeMillis();
        String fileName = "预约服务订单列表";
        reservationOrderService.downLoadSelection(request, response, vo, fileParentPath, fileName, hasPerms);
        String filepath = fileParentPath + File.separator + fileName + ".xlsx";
        FileDownloadUtil.downloadByFilepath(request, response, filepath);
    }

	/**
	 *
	 * @author WANGCONG
	 * 2022年3月1日上午09:52:01
	 * @desc 初始化构建查询VO对象
	 */
	public ReservationOrderVo initVo(Map<String, Object> reqVo) {
		ReservationOrderVo vo = new ReservationOrderVo();
		vo.setActivityType( PublicUtil.isNotEmpty(reqVo.get("activityType")) ? reqVo.get("activityType").toString() : null );
		vo.setActivityId( PublicUtil.isNotEmpty(reqVo.get("activityId")) ? reqVo.get("activityId").toString() : null );//服务项目id
        vo.setOrderNo( PublicUtil.isNotEmpty(reqVo.get("orderNo")) ? reqVo.get("orderNo").toString() : null );//服务订单号
        vo.setTel( PublicUtil.isNotEmpty(reqVo.get("tel")) ? reqVo.get("tel").toString() : null );//手机号
        vo.setCarNo( PublicUtil.isNotEmpty(reqVo.get("carNo")) ? reqVo.get("carNo").toString() : null );//车牌号
        vo.setCardNo( PublicUtil.isNotEmpty(reqVo.get("cardNo")) ? reqVo.get("cardNo").toString() : null );//卡券号、卡码
        vo.setOrderStatus( PublicUtil.isNotEmpty(reqVo.get("orderStatus")) ? reqVo.get("orderStatus").toString() : null );//订单状态
        vo.setServerId( PublicUtil.isNotEmpty(reqVo.get("serverId")) ? reqVo.get("serverId").toString() : null );//服务商
        vo.setStartTime( PublicUtil.isNotEmpty(reqVo.get("startTime")) ? reqVo.get("startTime").toString() : null ); //起止时间
        vo.setEndTime( PublicUtil.isNotEmpty(reqVo.get("endTime")) ? reqVo.get("endTime").toString() : null );
        vo.setPageNum( Integer.parseInt(String.valueOf(reqVo.get("pageNum"))) );  //分页
        vo.setPageSize( Integer.parseInt(String.valueOf(reqVo.get("pageSize"))) );
		return vo;
	}
		
	/**
	 *
	 * @author WANGCONG
	 * 2022年3月3日下午16:32:09
	 * @desc 查找服务商下拉列表
	 */
	@PostMapping(value = "/findServiceProvider")
    @ResponseBody
    @ApiOperation(httpMethod = "POST", value = "获取服务商列表")
    public Wrapper<String> findServiceProvider() {
        logger.info("获取服务商列表ReservationOrderController -- findServiceProvider()");
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> responseMsg = new HashMap<String, Object>();
		List<ValueAddedServiceProviderEntity> list = valueAddedServiceProviderService.queryProviderList();
        responseMsg.put("0", list);
        result.put("responseCode", "00");
        result.put("responseMsg", responseMsg);
        return WrapMapper.ok(JSON.toJSONString(result));
    }
	
	/**
	 *
	 * @author R
	 * 2022年4月8日 11:29:09
	 * @desc 获取服务项目列表
	 */
	@PostMapping(value = "/findActivity/{serviceType}/{channel}")
	@ResponseBody
	@ApiOperation(httpMethod = "POST", value = "获取服务项目")
	public Wrapper<String> findActivity(@ApiParam(name = "reqVo", value = "请求信息") @PathVariable String serviceType,
    		@ApiParam(name = "reqVo", value = "请求信息") @PathVariable String channel) {
		logger.info("获取服务项目列表 -- findActivity()");
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> responseMsg = new HashMap<String, Object>();
		List<StoreActivityConfigEntity> storeActivityConfigEntityList = null;
		
		//serviceType或者channel NULL是选中后删除 传递参数的占位符
		if(serviceType.equals("1")){//服务包
			if(channel.equals("ZXHY")||channel.equals("NULL")){//阳光
				//服务包对应的服务类型
				storeActivityConfigEntityList = storeActivityConfigService.queryActivityArr(); 
			}else{//国华
				storeActivityConfigEntityList = new ArrayList<StoreActivityConfigEntity>();
			}
		}else if(serviceType.equals("NULL")){ 
			if(channel.equals("ZXHY")){//阳光
				//服务包对应的服务类型
				storeActivityConfigEntityList = storeActivityConfigService.queryActivityArr(); 
			}else{//国华
				storeActivityConfigEntityList = new ArrayList<StoreActivityConfigEntity>();
			}
		}else{//单项
			if(channel.equals("GHMY")||channel.equals("NULL")){//国华
				//单项对应的服务类型
				List<DictOptionVo> dictOptionVoList = dictCodeService.getSelectOptions("singleActivityName", "1");
				storeActivityConfigEntityList = new ArrayList<StoreActivityConfigEntity>();
				//合并后的服务类型
				for (int i = 0; i < dictOptionVoList.size(); i++) {
					DictOptionVo dictOptionVo = dictOptionVoList.get(i);
					String code = dictOptionVo.getValue();
					String name = dictOptionVo.getLabel();
					StoreActivityConfigEntity storeActivityConfigEntity = new StoreActivityConfigEntity();
					storeActivityConfigEntity.setActivityCode(code);
					storeActivityConfigEntity.setActivityName(name);
					storeActivityConfigEntityList.add(storeActivityConfigEntity);
				}
			}else{//阳光
				storeActivityConfigEntityList = new ArrayList<StoreActivityConfigEntity>();
			}
		}
		
		responseMsg.put("0", storeActivityConfigEntityList);
		result.put("responseCode", "00");
		result.put("responseMsg", responseMsg);
       
		return WrapMapper.ok(JSON.toJSONString(result));
	}
}
